package wujinye.com.dao;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import wujinye.com.dao.base.BaseDao;
import wujinye.com.model.Dept;
import wujinye.com.utils.JDBCUtil;

import java.sql.SQLException;
import java.util.List;

public class DeptDao extends BaseDao {

    /**
     * 查找全部部门
     * @return List<Dept>部门对象列表
     */
    public List<Dept> findAll() {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        // 开启下划线->驼峰转换
        BasicRowProcessor processor = new BasicRowProcessor(new GenerousBeanProcessor());
        List<Dept>  list = null;
        try {
            list = qr.query("select * from dept;", new BeanListHandler<>(Dept.class, processor));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 通过部门ID查找部门名称
     * @param deptID
     */
    public Object[] findDeptNameById(Integer deptID) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "select name from dept where id = ?;";
        Object[] obj = null;
        try {
            obj = qr.query(sql,new ArrayHandler(),deptID);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return obj;
    }
}
